package com.meixiang.adapter.home;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.training.Training;
import com.meixiang.util.GlideHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author yinsujun  Created at 2016/5/12 13:57
 */
public class CosmeticTrainingAdapter extends RecyclerView.Adapter<CosmeticTrainingAdapter.ViewHolder> {

    private Context context;

    public List<Training> getData() {
        return data;
    }

    private List<Training> data=new ArrayList<>();

    public CosmeticTrainingAdapter(Context context) {
        this.context = context;
    }
    public void addAll(List<Training> data){
        this.data.addAll(data);
        this.notifyDataSetChanged();
    }public void clear(){
        this.data.clear();
        this.notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_cosmetic_training_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if(onItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    onItemClickListener.onItemClick(pos,holder.itemView);
                }
            });
        }
        if(data.size()>0){
            Training training = data.get(position);
            GlideHelper.showImage(context, training.getGoodsImage(),holder.cosmeticTrainingIv);
            holder.tvMoney.setText("￥"+training.getGoodsStorePrice());
            holder.tvName.setText(training.getGoodsName());
        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;

    }
    public interface OnItemClickListener{
        void onItemClick(int position,View view);
    }



    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.cosmetic_training_iv)
        ImageView cosmeticTrainingIv;
        @Bind(R.id.cosmetic_training_name)
        TextView tvName;
        @Bind(R.id.cosmetic_training_money)
        TextView tvMoney;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
